package com.qianfeng.shopping.dao;

import com.qianfeng.shopping.entity.LogLogin;
import com.qianfeng.shopping.util.ConnDb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LogLoginDaoImp implements  LogLoginDao{
    @Override
    public boolean insert(LogLogin logLogin) {
        Connection conn = ConnDb.getConnection();
        PreparedStatement pst = null;
        String sql="insert into loglogin(logloginip,loglogintime,accountid) values (?,?,?)";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,logLogin.getLogLoginIp());
            pst.setString(2,logLogin.getLogLoginTime());
            pst.setString(3,logLogin.getAccountId());
            return pst.executeUpdate()==1;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnDb.closeDb(null,pst,conn);
        }
        return false;
    }

    @Override
    public int selectLoginTimes() {
        Connection conn = ConnDb.getConnection();
        PreparedStatement pst = null;
        String sql="select count(logloginid) as logintimes from loglogin";
        ResultSet rs = null;

        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()){
                return rs.getInt("logintimes");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnDb.closeDb(null,pst,conn);
        }
        return 0;
    }

    public static void main(String[] args) {
        LogLoginDao dao = new LogLoginDaoImp();
        LogLogin logLogin = new LogLogin();
        logLogin.setLogLoginIp("192.168.1.123");
        logLogin.setLogLoginTime("20230306125736125");
        logLogin.setAccountId("35e1420d79804af8b0a53b4bda6170a6");
//        System.out.println(dao.insert(logLogin));
        System.out.println(dao.selectLoginTimes());
    }

}
